﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Dynamo
{
    public class Twitter
    {
        string Template = "<div class='TwitterStatus'><img height='50' valign='middle' src='[IMG]'><a target='_blank' href='http://twitter.com/[ScreenName]'>@[ScreenName]</a> - [Text]</div>";

        public string Timeline()
        {
            return Timeline(null);
        }
        public string Timeline(string ScreenName)
        {
            return Timeline(ScreenName, Template);
        }

        public string Timeline(string ScreenName, string InputTemplate)
        {
            if (ScreenName == null || ScreenName == "")
                return PublicTimeline(InputTemplate);
            else
                return UserTimeline(ScreenName, InputTemplate);
        }

        public string UserTimeline(string ScreenName)
        {
            return UserTimeline(ScreenName, Template);
        }
        public string UserTimeline(string ScreenName, string InputTemplate)
        {
            return EmitTimeline(Twitterizer.TwitterTimeline.UserTimeline(new Twitterizer.UserTimelineOptions() { ScreenName = ScreenName }), InputTemplate);
        }

        public string PublicTimeline()
        {
            return PublicTimeline(Template);
        }
        public string PublicTimeline(string InputTemplate)
        {

            return EmitTimeline(Twitterizer.TwitterTimeline.PublicTimeline(), InputTemplate);
        }
        public string EmitTimeline(Twitterizer.TwitterStatusCollection StatusCollection, string InputTemplate)
        {
            if (InputTemplate == null || InputTemplate == "") InputTemplate = this.Template;
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach (Twitterizer.TwitterStatus s in StatusCollection)
            {
                try
                {
                    string t = InputTemplate;

                    if (s.CreatedDate != null)
                        t = t.Replace("[CreatedDate]", s.CreatedDate.ToShortTimeString());
                    else
                        t = t.Replace("[CreatedDate]", "");

                    if (s.Place != null)
                        t = t.Replace("[Place]", s.Place.FullName);
                    else
                        t = t.Replace("[Place]", "");

                    t = t.Replace("[ScreenName]", s.User.ScreenName);
                    t = t.Replace("[IMG]", s.User.ProfileImageLocation);


                    t = t.Replace("[Text]", DNNJintEngine.ConvertUrlsToLinks(s.Text));
                    t = t.Replace("[Source]", s.Source);

                    sb.Append(t);
                }
                catch (Exception) { }
            }
            return sb.ToString();

        }
    }
}